import 'package:flutter/cupertino.dart' show CupertinoDialogAction;
import 'package:flutter/material.dart';

import '../base_stateless_widget.dart';

/// BaseDialogAction
/// use CupertinoDialogAction by cupertino
/// *** use cupertino = { forceUseMaterial: true } force use TextButton on cuperitno.
/// use TextButton by material
/// *** use material = { forceUseCupertino: true } force use CupertinoDialogAction on material.
class BaseDialogAction extends BaseStatelessWidget {
  const BaseDialogAction({
    Key? baseKey,
    this.key,
    this.onPressed,
    this.isDefaultAction = false,
    this.isDestructiveAction = false,
    this.textStyle,
    required this.child,
    this.onHighlightChanged,
    this.textColor,
    this.disabledTextColor,
    this.backgroundColor,
    this.foregroundColor,
    this.overlayColor,
    this.shadowColor,
    this.surfaceTintColor,
    this.elevation,
    this.focusElevation,
    this.hoverElevation,
    this.disabledElevation,
    this.iconColor,
    this.disabledIconColor,
    this.padding,
    this.visualDensity,
    this.shape,
    this.enableFeedback = true,
    this.alignment,
    this.splashFactory,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus = false,
    this.statesController,
    Map<String, dynamic>? cupertino,
    Map<String, dynamic>? material,
  }) : super(
      key: baseKey,
      cupertino: cupertino ?? const <String, dynamic>{},
      material: material ?? const <String, dynamic>{}
  );

  /// *** general properties start ***

  @override
  final Key? key;

  /// [CupertinoDialogAction.onPressed]
  /// or
  /// [TextButton.onPressed]
  final VoidCallback? onPressed;

  /// [CupertinoDialogAction.onPressed]
  /// or
  /// [TextButton.child]
  final Widget child;

  /// *** general properties end ***

  /// *** cupertino properties start ***

  /// [CupertinoDialogAction.isDefaultAction]
  final bool isDefaultAction;

  /// [CupertinoDialogAction.isDestructiveAction]
  final bool isDestructiveAction;

  /// [CupertinoDialogAction.textStyle]
  final TextStyle? textStyle;

  /// *** cupertino properties end ***

  /// *** material properties start ***

  /// [TextButton.onHover] (替代 onHighlightChanged)
  final ValueChanged<bool>? onHighlightChanged;

  /// [TextButton.style] 相关属性
  final Color? textColor;
  final Color? disabledTextColor;
  final Color? backgroundColor;
  final Color? foregroundColor;
  final Color? overlayColor;
  final Color? shadowColor;
  final Color? surfaceTintColor;
  final double? elevation;
  final double? focusElevation;
  final double? hoverElevation;
  final double? disabledElevation;
  final Color? iconColor;
  final Color? disabledIconColor;
  final EdgeInsetsGeometry? padding;
  final VisualDensity? visualDensity;
  final OutlinedBorder? shape; // 改为 OutlinedBorder? 类型
  final bool enableFeedback;
  final AlignmentGeometry? alignment;
  final InteractiveInkFeatureFactory? splashFactory;
  final Clip clipBehavior;
  final FocusNode? focusNode;
  final bool autofocus;
  final MaterialStatesController? statesController;

  /// *** material properties end ***

  @override
  Widget buildByCupertino(BuildContext context) {
    return CupertinoDialogAction(
      onPressed: valueFromCupertino('onPressed', onPressed),
      isDefaultAction: isDefaultAction,
      isDestructiveAction: isDestructiveAction,
      textStyle: textStyle,
      child: valueFromCupertino('child', child),
    );
  }

  @override
  Widget buildByMaterial(BuildContext context) {
    // 创建 TextButton 的样式
    final ButtonStyle style = TextButton.styleFrom(
      foregroundColor: textColor,
      disabledForegroundColor: disabledTextColor,
      backgroundColor: backgroundColor,
      disabledBackgroundColor: disabledTextColor,
      shadowColor: shadowColor,
      surfaceTintColor: surfaceTintColor,
      elevation: elevation,
      textStyle: textStyle,
      padding: padding,
      minimumSize: Size.zero,
      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
      visualDensity: visualDensity,
      shape: shape, // 现在类型匹配了
      enableFeedback: enableFeedback,
      alignment: alignment,
      splashFactory: splashFactory,
    );

    return TextButton(
      key: valueFromMaterial('key', key),
      onPressed: valueFromMaterial('onPressed', onPressed),
      style: style,
      focusNode: focusNode,
      autofocus: autofocus,
      clipBehavior: clipBehavior,
      statesController: statesController,
      onHover: onHighlightChanged,
      child: valueFromMaterial('child', child),
    );
  }
}